package gui;

import gui.helper.ColorEditor;
import gui.helper.ColorRenderer;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;

import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.LayoutStyle;
import javax.swing.WindowConstants;

import table.models.ColorChosingTableModel;
import core.Constants;
import core.CustomIcons;
import core.GuiComponentLabels;
import dataBase.DbHolder;

/**
 * This code was edited or generated using CloudGarden's Jigloo SWT/Swing GUI
 * Builder, which is free for non-commercial use. If Jigloo is being used
 * commercially (ie, by a corporation, company or business for any purpose
 * whatever) then you should purchase a license for each developer using Jigloo.
 * Please visit www.cloudgarden.com for details. Use of Jigloo implies
 * acceptance of these licensing terms. A COMMERCIAL LICENSE HAS NOT BEEN
 * PURCHASED FOR THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED LEGALLY FOR
 * ANY CORPORATE OR COMMERCIAL PURPOSE.
 */
public class ColorCustomizingWindow extends javax.swing.JFrame {
	private JTable colorSettingTable;
	private JScrollPane jScrollPane1;
	private JButton resetButton;
	private JButton saveButton;
	private JButton closeButton;
	private static ColorCustomizingWindow instance;

	/**
	 * Auto-generated main method to display this JFrame
	 */
	public static ColorCustomizingWindow getInstance() {
		if (instance == null) {
			instance = new ColorCustomizingWindow();
		}
		return instance;
	}

	private ColorCustomizingWindow() {
		super();
		this.setIconImage(CustomIcons.iconImage);
		this.initGUI();
	}

	private void initGUI() {
		try {
			GroupLayout thisLayout = new GroupLayout(this.getContentPane());
			this.getContentPane().setLayout(thisLayout);
			this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			this.setTitle(GuiComponentLabels.COLOR_CUSTOMIZING);
			{
				this.jScrollPane1 = new JScrollPane();
				{
					ColorChosingTableModel colorSettingTableModel = new ColorChosingTableModel();
					colorSettingTableModel.setColumnNames(new String [] { GuiComponentLabels.AGE_CLASS, GuiComponentLabels.BOW_CATHEGORY, GuiComponentLabels.GENDER, GuiComponentLabels.COLOR });

					String [] ageClasses = DbHolder.getInstance().getArcherData().getAllAgeClasses();
					String [] bowCathegories = DbHolder.getInstance().getArcherData().getAllBowCathegories();
					String [] gender = new String [] { Constants.MALE, Constants.FEMALE };

					for (int i = 0; i < ageClasses.length; i++) {
						for (int j = 0; j < bowCathegories.length; j++) {
							for (int k = 0; k < gender.length; k++) {
								colorSettingTableModel.addRow(new Object [] { ageClasses [i], bowCathegories [j], gender [k],
										DbHolder.getInstance().getArcherData().getColor(bowCathegories [j], ageClasses [i], gender [k]) });
							}
						}
					}
					this.colorSettingTable = new JTable();
					this.colorSettingTable.setAutoCreateRowSorter(true);
					this.jScrollPane1.setViewportView(this.colorSettingTable);
					this.colorSettingTable.setModel(colorSettingTableModel);
					this.colorSettingTable.setDefaultRenderer(Color.class, new ColorRenderer(true));
					this.colorSettingTable.setDefaultEditor(Color.class, new ColorEditor());
				}
			}
			{
				this.closeButton = new JButton();
				this.closeButton.setText(GuiComponentLabels.CLOSE_BUTTON);
				this.closeButton.setIcon(CustomIcons.exit);
				this.closeButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent evt) {
						// /** close every single colorchoser -> column 3 */
						ColorChosingTableModel model = (ColorChosingTableModel) ColorCustomizingWindow.this.colorSettingTable.getModel();
						for (int i = 0; i < model.getRowCount(); i++) {
							model.getValueAt(i, 3);
						}
						instance = null;
						ColorCustomizingWindow.this.dispose();
					}
				});
			}
			{
				this.saveButton = new JButton();
				this.saveButton.setText(GuiComponentLabels.SAVE_BUTTON);
				this.saveButton.setIcon(CustomIcons.save);
				this.saveButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent evt) {
						Color color;
						String bowCathegory;
						String ageClass;
						String gender;
						for (int i = 0; i < ColorCustomizingWindow.this.colorSettingTable.getModel().getRowCount(); i++) {
							color = (Color) ColorCustomizingWindow.this.colorSettingTable.getModel().getValueAt(i, 3);
							bowCathegory = ColorCustomizingWindow.this.colorSettingTable.getModel().getValueAt(i, 1).toString();
							ageClass = ColorCustomizingWindow.this.colorSettingTable.getModel().getValueAt(i, 0).toString();
							gender = ColorCustomizingWindow.this.colorSettingTable.getModel().getValueAt(i, 2).toString();

							try {
								DbHolder.getInstance().getArcherData().setColor(color, bowCathegory, ageClass, gender);
							} catch (SQLException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
						}
					}
				});
			}
			thisLayout.setVerticalGroup(thisLayout
					.createSequentialGroup()
					.addContainerGap()
					.addComponent(this.jScrollPane1, GroupLayout.PREFERRED_SIZE, 286, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
					.addGroup(
							thisLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
									.addComponent(this.closeButton, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
									.addComponent(this.saveButton, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
									.addComponent(this.getResetButton(), GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, 23, GroupLayout.PREFERRED_SIZE)).addContainerGap());
			thisLayout.setHorizontalGroup(thisLayout
					.createSequentialGroup()
					.addContainerGap()
					.addGroup(
							thisLayout
									.createParallelGroup()
									.addGroup(
											GroupLayout.Alignment.LEADING,
											thisLayout.createSequentialGroup().addComponent(this.getResetButton(), 0, 90, Short.MAX_VALUE)
													.addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED, 1, GroupLayout.PREFERRED_SIZE)
													.addComponent(this.saveButton, GroupLayout.PREFERRED_SIZE, 122, GroupLayout.PREFERRED_SIZE)
													.addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED, 1, GroupLayout.PREFERRED_SIZE)
													.addComponent(this.closeButton, GroupLayout.PREFERRED_SIZE, 122, GroupLayout.PREFERRED_SIZE))
									.addComponent(this.jScrollPane1, GroupLayout.Alignment.LEADING, 0, 356, Short.MAX_VALUE)).addContainerGap());

			this.pack();
			this.setSize(396, 382);
		} catch (Exception e) {
			// add your error handling code here
			e.printStackTrace();
		}
	}

	private JButton getResetButton() {
		if (this.resetButton == null) {
			this.resetButton = new JButton();
			this.resetButton.setText("reset");
			this.resetButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent evt) {
					try {
						DbHolder.getInstance().getArcherData().deleteColorTable();
						DbHolder.getInstance().getArcherData().createColorTable();
					} catch (SQLException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			});
		}
		return this.resetButton;
	}
}
